package com.macrog.rabbitmq.publish;

import com.rabbitmq.client.*;
import com.macrog.rabbitmq.utils.ConnectionFactoryUtil;

import java.io.IOException;

/**
 * Created by Administrator on 2018/9/2.
 */
public class Recv0 {

    private static final String QUEUE_NAME = "publishQueue0";
    private static final String EXCHANGE_NAME = "publishExchange";

    public static void main(String[] args) throws IOException {
        Connection connection = ConnectionFactoryUtil.getConnection();
        final Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");

        //告诉服务器在我没有确认消息完成之前不要给我发送新的消息
        channel.basicQos(1);

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者111111：" + new String(body, "UTF-8"));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        channel.basicConsume(QUEUE_NAME, false, consumer);

    }
}
